package com.olt.server.user;

import cn.hutool.core.util.IdUtil;
import com.olt.dao.user.*;
import com.olt.domain.user.OltCustom;
import com.olt.domain.user.OltPower;
import com.olt.domain.user.OltUser;
import com.olt.domain.user.UserRole;
import com.olt.server.email.EmailService;
import com.olt.server.user.dto.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import util.*;

import java.util.Date;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

@Service
public class UserServer {
    @Autowired
    IPowerDao iPowerDao;
    @Autowired
    EmailService emailService;

    @Autowired
    UserDao userDao;
    @Autowired
    ICustomDao iCustomDao;

    @Autowired
    IUserRole iUserRole;


    //线程池
    ExecutorService executorService = Executors.newCachedThreadPool(new ThreadPoolFactory("cachedThread"));

    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    @Autowired
    IRoleDao iRoleDao;



    public OltPower findPowerByName(String name){
      return   iPowerDao.getByPowerName(name);
    }

    public void  insertPower(OltPower power){
         iPowerDao.save(power);
    }

    public boolean  sendEmail(SendEmailInput input){
        try {
            String code = VerifyCode.getNewVerifyCode(6);
            System.out.println(code);
            executorService.submit(()->{
                emailService.sendSimpleMail(input.email, "在线考试平台验证码", "你的验证码是:"+code);
            });
            redisTemplate.opsForValue().set(input.getEmail(),code,300, TimeUnit.SECONDS);
            return true;
        }
        catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }

    public boolean register(RegitInput input){
       var str= redisTemplate.opsForValue().get(input.email);
       if(StringUtil.isEmptyOrNull(str)|| !str.equals(input.verifyCode)){
           return false;
       }
        OltUser user=new OltUser();
        user.setUserName(input.email);
        user.setHashPassword(input.pass);
        user.setUserEnabled(true);
        user.setUserType("user");
        user.setUserAccountNotExpired(true);
        user=userDao.save(user);
        OltCustom custom=new OltCustom();
        custom.setEmail(input.email);
        custom.setUserId(user.getUserId());
        custom.setName(input.email);
        custom.setCustomId(IdUtil.simpleUUID());
        custom.setRegditTime(new Date());
        iCustomDao.save(custom);
        UserRole userRole=new UserRole();
        userRole.setRoleId(input.type);
        userRole.setUserId(user.getUserId());
        iUserRole.save(userRole);


        return true;
    }

    public  OltCustom findOltCustom(Long userId){
        return  iCustomDao.findByUserId(userId);
    }

    public GetUserForEdit findUserForeEdit(String id){
        var res=new GetUserForEdit();
        var dest=iCustomDao.findByCustomId(id);
        MapperUtil.MapperClass(res,dest);
        return res;
    }

    public Boolean editUser(GetUserForEdit input){
        var dest=iCustomDao.findByCustomId(input.getCustomId());
        dest.setName(input.getName());
        dest.setSex(input.getSex());
        dest.setTel(input.getTel());
        dest.setImgUrl(input.getImgUrl());
        iCustomDao.save(dest);
        return true;

    }

    public RoleDto getRoleByUserId(Long userId){
        return new RoleDto(iRoleDao.getRoleNameByUserId(userId));
    }

    public PermissionDto getPowerByRole(List<Long> role){
        return new PermissionDto(iPowerDao.findByRoleId(role));

    }







}
